package com.daka.service.impl;

import com.daka.dao.GroupMapper;
import com.daka.entity.Group;
import com.daka.entity.GroupVolunteer;
import com.daka.entity.User;
import com.daka.entity.Volunteer;
import com.daka.service.GroupService;
import com.daka.service.GroupVolunteerService;
import com.daka.service.UserService;
import com.daka.service.VolunteerService;
import com.daka.service.qnzyz.BHMember;
import com.daka.service.qnzyz.BHMemberService;
import com.daka.service.qnzyz.BHOrgService;
import com.daka.tools.DKQConstant;
import com.daka.tools.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangx
 * Date: 2016/7/28 0028
 * Time: 17:35
 */
@Service("groupService")
@Transactional
public class GroupServiceImpl implements GroupService{
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private BHOrgService bhOrgService;
    @Autowired
    private BHMemberService bhMemberService;
    @Autowired
    private VolunteerService volunteerService;
    @Autowired
    private UserService userService;
    @Autowired
    private GroupVolunteerService groupVolunteerService;



    public int insert(Group group,long userId){

        Volunteer volunteer = new Volunteer();
        volunteer.setUserId(Long.valueOf(userId));
        volunteer = volunteerService.queryByEntity(volunteer);
        if (volunteer != null) {
            group.setCreateUser(volunteer);
        } else {
            return 0;
        }
        if(group.getServiceField()!=null){
            group.setServiceField(group.getServiceField().replace(",", ";"));
        }

        group.setCreateTime(new Timestamp(new Date().getTime()));
        group.setTimes(0);
        group.setVolunteers(1);
        group.setMonitor(group.getCreateUser().getRealName());
        group.setPhone(group.getCreateUser().getMobile());

        //提交到北航
        Map<String, String> map = new HashMap<String, String>();
        map.put("groupName", group.getName());
        map.put("groupAdmCode", group.getCode());
        map.put("groupProvince", group.getProvince());
        map.put("groupCity", group.getCity());
        map.put("groupDistrict", group.getDistrict());
        map.put("groupAddress", group.getRegAddress());
        map.put("groupPostcode", "");
        map.put("groupAdmMobile", "");
        map.put("groupAdmPwd", "");
        map.put("groupAdmName", group.getMonitor());
        map.put("groupAdmIdNo", "");
        map.put("groupAdmTel", group.getCreateUser().getMobile());
        map.put("groupCategory", "");
        map.put("groupSummary", group.getDemo());
        try {
            String groupCode = bhOrgService.reg4Social(map);
            if (StringUtils.isNotBlank(groupCode)) {
                group.setGroupCode(groupCode);
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                throw e;
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        //保存数据
        groupMapper.insert(group);

        try {
            //志愿者一号通code
            String voluteerCode = volunteer.getVolunteerCode();
            if (StringUtils.isBlank(volunteer.getVolunteerCode()) || "null".equals(volunteer.getVolunteerCode())) {
                voluteerCode = bhMemberService.simpleReg(BHMember.toBHMember(volunteer, group.getGroupCode()));
                volunteer.setVolunteerCode(voluteerCode);
                volunteerService.update(volunteer);
            }
            //加入组织
            if (StringUtils.isNoneBlank(volunteer.getVolunteerCode())) {
                GroupVolunteer groupVolunteer = new GroupVolunteer();
                groupVolunteer.setCreateTime(new Timestamp(new Date().getTime()));
                groupVolunteer.setStatus(DKQConstant.APPLY_STATUS_OK);
                groupVolunteer.setRole(DKQConstant.ROLES_CREATEER);
                groupVolunteer.setTimes(0l);
                groupVolunteer.setDelStatus(0);
                groupVolunteer.setGroupId(group.getId());
                groupVolunteer.setVolunteerId(volunteer.getId());
                groupVolunteerService.insert(groupVolunteer);


                try {
                    //????????????????????
                    bhMemberService.joinGroup(voluteerCode, group.getGroupCode());
                    //??????????????,?????????????????????????????????????й??????

                    String password = "";
                    User userP = userService.selectByPrimaryKey(userId);
                    if (StringUtils.isNotBlank(userP.getPassword())) {
                        password = userP.getPassword();
                    } else {
                        password = MD5Util.MD5("123456");
                    }
                    bhOrgService.addManager(group.getGroupCode(), group.getCreateUser().getRealName(), group.getCreateUser().getMobile(), password, group.getName(), group.getCreateUser().getCardno(), group.getPhone());

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

        return 1;
    }

    public int update(Group group){
        return groupMapper.updateByPrimaryKeySelective(group);
    }

    public List<Group> queryListByVolunteer(long volunteerId){
        return groupMapper.queryListByVolunteer(volunteerId);
    }

    public List<Group> selectByStatement(Group po){
        return groupMapper.selectByStatement(po);
    }

    public Group selectByPrimaryKey(Long id){
        return groupMapper.selectByPrimaryKey(id);
    }

}
